package com.ph.shopping.common.util.oss;

import com.aliyun.oss.ServiceException;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Discription 图片上传工具工具类
 * @Author 舒豪
 * @Date 2017-3-20 下午3:03:33
 */
public class UpLoadUtils {

    private static final Log logger = LogFactory.getLog(UpLoadUtils.class);

    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

    public static String BASE_PATH;
    //imageUploadPath 图片上传路径
    public static String IMAGE_UPLOAD_PATH;

    public UpLoadUtils() {
        UpLoadUtils.BASE_PATH = request.getRealPath("/");
        UpLoadUtils.IMAGE_UPLOAD_PATH = UpLoadUtils.BASE_PATH + "/upload/";
    }

    public static String getSerial(Date date, int index) {
        long msel = date.getTime();
        SimpleDateFormat fm = new SimpleDateFormat("MMddyyyyHHmmssSS");
        msel += index;
        date.setTime(msel);
        String serials = fm.format(date);
        return serials;
    }

    // 检查是否是图片格式
    public static boolean checkIsImage(String imgStr) {
        boolean flag = false;
        if (imgStr != null) {
            if (imgStr.equalsIgnoreCase(".gif")
                    || imgStr.equalsIgnoreCase(".jpg")
                    || imgStr.equalsIgnoreCase(".jpeg")
                    || imgStr.equalsIgnoreCase(".png")) {
                flag = true;
            }
        }
        return flag;
    }

	/*public static Date StrToDate(String str) throws ParseException {
        return new SimpleDateFormat("MM/dd/yyyy").parse(str);
	}

	public static Result saveFile(Map<String, String> imageMap, String module) {// 内部调用上传
		logger.info("=========================================saveFile方法========================");

		// imageUploadPath 图片上传路径
		Result appResult = new Result();
		appResult.setCode(0);//默认是失败
		String extName = ".jpg"; // 保存文件拓展名
		if(module.equals("Android")){
			extName=".apk";
		}else if(module.equals("IOS")){
			extName=".ipa";
		}
		String newFileName = ""; // 保存新的文件名
		String nowTimeStr = ""; // 保存当前时间
		SimpleDateFormat sDateFormat;
		Random r = new Random();
		String realPath = BASE_PATH;//实际路径
		String savePath = IMAGE_UPLOAD_PATH + module + "/";// 上传目录
		Set<Entry<String, String>> set = imageMap.entrySet();
		Map<String, String> resultMap = new HashMap<String, String>();
		for (Entry<String, String> entry : set) {
			String imageName = entry.getKey();
			String imageStr = entry.getValue();
			byte[] uploadImage = decode(imageStr);
			FileOutputStream fos = null;
			File fileupload = null;
			try {
				fileupload = new File("temp.te");
				if (!fileupload.exists()) {
					fileupload.createNewFile();
				}
				logger.info(savePath + "_______________size:"
						+ uploadImage.length + "====================="
						+ realPath);
				fos = new FileOutputStream(fileupload);
				fos.write(uploadImage);
				fos.flush();
				fos.close();
				// 获取文件大小
				long fileSize = fileupload.length();
				// 1m=1024*1024
				if (fileSize > 1024 * 1024*30) {
					appResult.setMsg(imageName + "上传的文件不能大于30M");
				} else {
					// 生成随机文件名：当前年月日时分秒+五位随机数（为了在实际项目中防止文件同名而进行的处理）
					int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000; // 获取随机数
					sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); // 时间格式化的格式
					nowTimeStr = sDateFormat.format(new Date()); // 当前时间
					// 获取拓展名
					newFileName = nowTimeStr + rannum + extName; // 文件重命名后的名字
					String filePath = savePath + newFileName;
					filePath = filePath.replace("\\", "/");
					String img_url = "upload/" + module + "/" + newFileName;
					// 检查上传的是否是图片
					// if (UpLoadUtils.checkIsImage(extName)) {
					FileUtils.copyFile(fileupload, new File(filePath));
					//alyUpload(module,newFileName,realPath+img_url,InitParamPc.getInitParam());
					appResult.setMsg(imageName + "，上传成功");
					resultMap.put(imageName, img_url);
					appResult.setCode(1);
					// } else {
					// appResult.setError_info(imageName
					// + "上传的文件类型错误，请选择jpg,jpeg,png和gif格式的图片!");
					// }

				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				appResult.setMsg(imageName + "上传失败，出错啦!"
						+ e.getMessage());
			} finally {

				try {
					if (fos != null) {
						fos.close();
					}
					fileupload.delete();
				} catch (IOException e) {
					//e.printStackTrace();
				}
			}
		}
		appResult.setContent(resultMap);
		return appResult;
	}

	public static String encode(byte[] byteArray) {
		BASE64Encoder base64Encoder = new BASE64Encoder();
		return base64Encoder.encode(byteArray);
	}

	public static byte[] decode(String base64EncodedString) {
		BASE64Decoder base64Decoder = new BASE64Decoder();
		try {
			return base64Decoder.decodeBuffer(base64EncodedString);
		} catch (IOException e) {
			return null;
		}
	}

	public static String getImageBase64(String image) {
		String base64str = "";
		base64str = encode(getBytes(image));
		return base64str;
	}

	*//**
     * 获得指定文件的byte数组
     *//*
    public static byte[] getBytes(String filePath) {
		byte[] buffer = null;
		try {
			File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			//e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		return buffer;
	}
	*/
    public static synchronized String getOrder() {
        return DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS") + RandomStringUtils.randomNumeric(6);
    }

    /**
     * springMvc上传
     * @param urlfile
     * @param module
     * @return
     */
    public Object springUploadFile(MultipartFile urlfile, String module) {
        String fileName = null;
        if (module == null || "".equals(module)) {
            module = "default";
        }

        try {
			if (urlfile.isEmpty()) {
                return 0;
            } else {
                String[] paramsName = (new String[]{"phshopping-api-platform", "phshopping-api-platform/upload"});
                String basePath = getDirection(paramsName[0], paramsName[1]) + "/" + module;
                File f = new File(basePath);
                if (!f.exists()) {
                    f.mkdir();
                }
                String path = null;
				fileName = urlfile.getOriginalFilename();
				boolean isFile= StringUtils.endsWithAny(StringUtils.lowerCase(fileName), new String[] { ".png", ".jpg",".jpeg",".bmp",".gif"});
                String sysFileName = getOrder() + "." + StringUtils.substringAfter(fileName, ".");
                if (isFile) {
					path = basePath + "/" + sysFileName;
				} else {
                    return 0;
                }
				urlfile.transferTo(new File(path));
                UpLoadUtils.alyUpload(module, sysFileName, path);
                Object address = "upload/" + module + "/" + sysFileName;
                return address;
            }
			} catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /***
     * 上传到图片服务器
     * @param module  上传的文件夹名
     * @param sysFileName 图片名
     * @param path 路劲
     */
    public static void alyUpload(String module, String sysFileName, String path/*,InitParamPc initParam*/) {
		 /*String isOpen=initParam.getProperties().getProperty("OSS_IS_OPEN");
		 String accessId=initParam.getProperties().getProperty("OSS_ACCESS_ID");
		 String accessKey=initParam.getProperties().getProperty("OSS_ACCESS_KEY");
		 String bucketName=initParam.getProperties().getProperty("OSS_BUCKET_NAME");*/

        String isOpen = "true";
        String accessId = "e6wt7YMUZEru6TOD";
        String accessKey = "QLhDKpV0Rez9fsH6oIOXbmks4sd7WE";
        String bucketName = "yst-images";
        if ("true".equals(isOpen))// 是否开启sso********把图片保存到云服务器上
        {
            OSSFileUpLoad.createOss(bucketName, "upload/" + module + "/" + sysFileName, path, accessId, accessKey);
        }
    }

    private static final String CURRT_CLASS_ROOT = UpLoadUtils.class.getClassLoader().getResource("").toString();

    /**
     * <DL>
     * <DD>根据指定名称返回路径值.</DD><BR>
     * </DL>
     *
     * @param paramsName 参数名
     * @param flag       标识
     * @return 路径值
     * @author Angma <ZhouPeng>
     * @date 2016年9月9日
     */
    public Object getRoot(String[] paramsName, int flag) {

        //通过类的反射机制调用方法
        for (int i = 0; i < paramsName.length; i++) {
            String methodName = "";
            try {
                methodName = "get" + paramsName[i];
                Object value = this.getClass().getMethod(methodName, new Class[]{}).invoke(this, new Object[]{});
                paramsName[i] = value.toString();
            } catch (NoSuchMethodException e) {
                throw new ServiceException("方法名: [" + methodName + "]不存在!", e);
            } catch (IllegalAccessException e) {
                throw new ServiceException("不能够访问方法: [ " + methodName + "]!", e);
            } catch (InvocationTargetException e) {
                throw new ServiceException("不能够访问方法: [ " + methodName + "], 对象可能未实例化!", e);
            }
        }

        //返回值或者路径
        if (flag == 0) {
            return paramsName;
        }
        return getDirection(paramsName[0], paramsName[1]);
    }

    /**
     * <DL>
     * <DD>获取资料存储根目录位置.</DD><BR>
     * </DL>
     *
     * @param itemName itemName
     * @param root     root
     * @return 资料存储根目录位置
     * @author Angma <ZhouPeng>
     * @date 2016年9月12日
     */
    public static String getDirection(String itemName, String root) {
        String workDir = "c:/tmp";
        try {
            workDir = java.net.URLDecoder.decode(CURRT_CLASS_ROOT, "utf-8");
            String os = System.getProperties().getProperty("os.name");
            String sep = "";
            if (os.startsWith("Linux") || os.startsWith("linux")) {
                sep = "/";
            }
            String temp = workDir.substring(workDir.indexOf("/"), workDir.indexOf(itemName));
            workDir = sep + temp + root;
            logger.info("getDirection(" + itemName + "," + root + ")--path----->>:" + workDir);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            e.getMessage();
//            throw ("获取根目录失败："+e.getMessage());
        }
        return workDir;

    }



}